<?php
/**
 * 消息模型
 * @author MuTao <961313085@qq.com>
 * */
namespace app\common\model;
use think\Model;
class Message extends Model{
	
	/**
	 * 获取最新的一条或多条未读消息
	 * @param string $uid 接受用户唯一id
	 * @param string $field 字段
	 * @param int $num 条数
	 * @return array
	 * */
	public function getUnreadMsg($uid, $num=0, $field='*') {
		$res = self::field($field)
				->where(['receiver' => $uid, 'is_delete' => '0'])
				->order('id desc');
		if($num){
			$res = $res->limit($num);
		}
		$res = $res->limit($num)->select();

		$ret = [];
		foreach ($res as $v) $ret[] = $v->getData();
		return $ret;
	}
	
	/**
	 * 消息列表
	 * @param string $uid 接受用户唯一id
	 * @param int num 每页显示条数
	 * @param array 
	 * */
	public function msgList($uid, $num=15){
		return self::where(['receiver' => $uid, 'is_delete' => '0'])->order('sendtime desc')->paginate($num);
	}

    /**
     * 微信消息列表
     * @param int $uid 用户id
     * @param bool $filed 字段
     * @param int $page  页数量
     * @param int $pagesize 每页显示条数
     * @return array|false
     */
    public function appmsgList($uid,$filed=false,$page=1,$pagesize=15){
        $result=self::where(['receiver' => $uid, 'is_delete' => '0'])->order('sendtime desc');
        if($filed) {
            $result=$result->field($filed);
        } else {
            $result=$result->field('*');
        }
        $pageStart = $page==1 ? 0 : ($page-1)*$pagesize;
        return $result = $result->limit($pageStart,$pagesize)->select();

    }

    /**
     * 消息列表
     * @param array $where 条件
     * @param int $num 每页显示条数
     * @param int $filed 字段
     * @param array
     * */
    public function msgLists($where,$num=15,$pageParam=[], $filed=false){
        $result=self::where($where)->order('sendtime desc');
        if($filed) $result=$result->field($filed); else $result=$result->field('*');
        return $result->paginate($num,false,$pageParam);
    }

	
	/**
	 * 获取未读消息总条数
	 * @param string $uid 接受用户唯一id
	 * @return int 消息数量
	 * */
	public function getUnreadMsgNum($uid) {
		return self::where(['receiver' => $uid, 'receivetime' => '0', 'is_delete' => '0'])->count();
	}
	
	/**
	 * 获取所有的未读消息
	 * @param string $uid 接受用户唯一id
	 * @param string $field 字段
	 * @return array  没有消息则返回空数组
	 * */
	public function getAllUnreadMsg($uid, $field='*') {

		$where['receiver'] = $uid;
		$where['is_delete'] = 0;
		$where['receivetime'] = 0;

		$res = self::field($field)->where($where)->order('id desc')->select();
		$ret = [];
		foreach ($res as $v) $ret[] = $v->getData();
		return $ret;
	}
	
	/**
	 * 发送一条
	 * @param string $sender 发送人uid 
	 * @param string $receiver 接收人uid
	 * @param string $content 消息主体
	 * @param string $type 消息类型,默认为1
	 * @param string $link 备注字段
	 * @return mixed 成功返回主键id,失败返回false
	 * */
	public function sendMessage($sender, $receiver, $content, $type=1, $remark, $link='') {
		try {
			return  self::insertGetId([
						'sender' => $sender,
						'receiver' => $receiver,
						'sendtime' => time(),
						'type' => $type,
						'content' => $content,
						'remark' => $remark,
						'link' => $link,
					]);
		}catch (\Exception $e) {return false;}
	}
	
	/**
	 * 将某条消息标记为已读
	 * @param string $id 要删除消息的主键id
	 * @return boolean
	 * */
	public function readMsgById($id) {
		if(false === self::where(['id' => $id])->update( ['receivetime' => time()])) return false;
		else return true;
	}
	
	/**
	 * 将某条消息逻辑删除
	 * @param array $id 要删除消息的主键id
	 * @return boolean
	 * */
	public function delMsgById($id) {
		if(false === self::where('id','in',$id)->update( ['is_delete' => 1, 'receivetime' => time()])) return false;
		else return true;
	}
	
	/**
	 * 彻底删除某条消息
	 * @param string $id 要删除消息的主键id
	 * @return boolean
	 * */
	public function clearMsgById($id) {
		if(false === $this->delete($id)) return false;
		else return true;
	}

	/**
	 * 将某用户下 所有 消息标记为已读
	 * @param  [type] $uid [description]
	 * @return [type]      [description]
	 */
	public function updOld($uid){
		$return = self::where(['receiver'=>$uid])->update(['receivetime'=>time()]);

		return $return;
	}
}